package com.agou.ssh.web.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.agou.core.enums.EnTreeNodeType;
import com.agou.core.ss.SSException;
import com.agou.core.ss.SSLogicException;
import com.agou.core.util.UtString;

/**
 * jquery easy ui tree 工具
 * @author agou
 *
 */
public class JqueryTree {
	
	public Map<String,TreeNode> treeMap = new HashMap<String,TreeNode>();//结点索引
	public Map<String,TreeNode> tempMap = new HashMap<String,TreeNode>();//临时使用
	private List<String> treeList = new ArrayList<String>();//
	private boolean isSuccess = false;
	
	public JqueryTree(){//构建实例
	}
	
	public void addTreeNode(TreeNode subTree) throws SSException{//添加节点
		if(isSuccess == true){
			SSLogicException lc = new SSLogicException(1,"已设置添加结束，不能够在添加新的元素.");
			throw lc;
		}
		tempMap.put(subTree.id, subTree);
		if(UtString.isEmpty(subTree.pid) || "-1".equals(subTree.pid)){//如果元素父元素是空。直接添加到根元素下
			treeMap.put(subTree.id, subTree);
		}else{//存在父元素
			TreeNode pTree = treeMap.get(subTree.pid);//取得父亲元素
			if(pTree == null){//还未遍历到父亲元素中
				treeList.add(subTree.id);
				treeMap.put(subTree.id, subTree);
			}else{//父亲元素存在
				pTree.addSubTreeNode(subTree);//添加子元素
			}
		}
	}
	
	public void endAdd(){//添加结束，对未设置完成的元素进行设置
		isSuccess = true;
		String treeId;
		TreeNode subTree;
		TreeNode pTree;
		for(int i = 0 ; i < treeList.size() ; i++){//对所有未链接的元素进行链接
			treeId = treeList.get(i);
			subTree = treeMap.get(treeId);
			pTree = treeMap.get(subTree.pid);
			if(pTree == null){//已配对
				pTree = tempMap.get(subTree.pid);
			}
			pTree.addSubTreeNode(subTree);//将子元素添加到父元素中
			treeMap.remove(treeId);//移除子元素
		}
		treeList = new ArrayList<String>();//重置
		tempMap = new HashMap<String,TreeNode>();//重置
	}
	
	public String generationHtml() throws SSException{
		if(isSuccess == false){
			SSLogicException lc = new SSLogicException(1,"请先设置结束添加标志.");
			throw lc;
		}
		StringBuilder sbHtml = new StringBuilder();
		java.util.Iterator<String> iter = treeMap.keySet().iterator();
		TreeNode tree;
		List<TreeNode> subList;
		while(iter.hasNext()){//进入循环
			tree =  treeMap.get(iter.next()) ;//
			if(tree.type.getCode() == EnTreeNodeType.Link.getCode()){//是链接
				sbHtml.append("<li id=\"").append(tree.id).append("\" data-options=\"state:'open'\"><span id='A20'>").append(tree.name).append("</span></li>").append("\n");
			}else{
				sbHtml.append("<li id=\"").append(tree.id).append("\" data-options=\"state:'open'\"><span id='A20'>").append(tree.name).append("</span>").append("\n");
				subList = tree.getSubTreeNode();
				if(subList.size() > 0){
					sbHtml.append("<ul>").append("\n");
					generationHtml(sbHtml,subList);
					sbHtml.append("</ul>").append("\n");
				}
				sbHtml.append("</li>").append("\n");
			}
		}
		return sbHtml.toString();
	}
	
	public void generationHtml(StringBuilder sbHtml,List<TreeNode> treeList){
		TreeNode subTree;
		for(int i = 0 ; i < treeList.size() ; i++){
			subTree = treeList.get(i);
			if(subTree.type.getCode() == EnTreeNodeType.Link.getCode()){//是链接
				sbHtml.append("<li id=\"").append(subTree.id).append("\" data-options=\"state:'open'\"><span id='A20'>").append(subTree.name).append("</span></li>").append("\n");
			}else{
				sbHtml.append("<li id=\"").append(subTree.id).append("\" data-options=\"state:'open'\"><span id='A20'>").append(subTree.name).append("</span>").append("\n");
				List<TreeNode> subList = subTree.getSubTreeNode();
				if(subList.size() > 0){
					sbHtml.append("<ul>").append("\n");
					generationHtml(sbHtml,subList);
					sbHtml.append("</ul>").append("\n");
				}
				sbHtml.append("</li>");
			}
			
		}
	}
}
